﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CMN.Entity;
using CMN.Common;
using CMN.Framework;
using CMN.BLL;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
namespace CMN.UI
{
    /// <summary>
    /// 友情链接
    /// </summary>
    public class Normal_VoteDisplay
    {
        static Normal_VoteDisplay()
        {
            BaseTemplateDisplay.OnEndDisplay+=new EventHandler<TemplateDisplayArg>(BaseTemplateDisplay_OnEndDisplay);
        }

     
        

        /// <summary>
        /// 友情链接绑定模板
        /// </summary>
        /// <param name="o"></param>
        /// <param name="arg"></param>
        private static void BaseTemplateDisplay_OnEndDisplay(object o, TemplateDisplayArg arg)
        {
            Normal_VoteDisplay searchContentDisplay = new Normal_VoteDisplay();
            arg.NvelocityTemplate.Put("vote", searchContentDisplay);
        }
        /// <summary>
        /// 获取公告列表
        /// </summary>
        /// <param name="showNum"></param>
        /// <param name="cacheSecends">缓存秒数</param>
        /// <returns></returns>
        public List<Normal_VoteEntity> List(string id, int cacheSecends)
        {
          return   List(id, cacheSecends, true);
        }


        /// <summary>
        /// 获取公告列表
        /// </summary>
        /// <param name="showNum"></param>
        /// <param name="cacheSecends">缓存秒数</param>
        /// <returns></returns>
        public List<Normal_VoteEntity> List(string id, int cacheSecends,bool isHasStartDate)
        {
            QueryParam qp = new QueryParam();
            qp.PageSize = 100;
            qp.PageIndex = 1;
            qp.Orderfld = "AddDate desc ";
            qp.Where = "Enabled=1  ";
            BLLHelper bll = new BLLHelper();
            if (isHasStartDate)
            {
                qp.Where = string.Concat(qp.Where, " and StartDate<=", bll.GetSqlDateTime(DateTime.Now), " and enddate>= ", bll.GetSqlDateTime(DateTime.Now));
            }

            if (id.IndexOf(',') > -1)
            {
                qp.Where = string.Concat(qp.Where, " and id in(", id, ")");

            }
            else
                qp.Where = string.Concat(qp.Where, "  id =", id);
            List<Normal_VoteEntity> noticeList = MvcWebCacheManager.GetNormal_VoteList(qp, cacheSecends);
            List<VoteInfo> voteList = new List<VoteInfo>();
            foreach (Normal_VoteEntity voteEntity in noticeList)
            {
                 voteList = (List<VoteInfo>)JsonConvert.DeserializeObject(voteEntity.VoteText, typeof(List<VoteInfo>));
                 voteEntity.VoteInfos = voteList;
            }
            
            return noticeList;
        }


    }

}
